class_name GameManagerr
extends BaseSystem

@export var game_mode:GameEnum.GameMode = GameEnum.GameMode.Dev
@export_group("随机数")
@export var seed:int
@export_group("")
@export var fsm: FSM
var rng:RandomNumberGenerator
var _last_game_state:GameEnum.GameState = GameEnum.GameState.Null
var _cur_game_state:GameEnum.GameState = GameEnum.GameState.Null
var last_rng_state = null
var blackboard_data:BlackboardData

#region 基于固定种子的随机数
func rng_init():
	rng = RandomNumberGenerator.new()
	rng_randomize()
func rng_randf() -> float:
	return rng.randf()
func rng_randf_range(from:float,to:float) -> float:
	return rng.randf_range(from,to)
func rng_randi() -> int:
	return rng.randi()
func rng_randi_range(from:int,to:int) -> int:
	return rng.randi_range(from,to)
func rng_fixed_seed():
	rng.seed = self.seed
func rng_randomize():
	last_rng_state = rng.state
	rng.randomize()
func rng_restore():
	if last_rng_state:
		rng.state = last_rng_state
#endregion
func get_blackboard_data(key:String):
	return blackboard_data.get_data(key)
func set_blackboard_data(key:String,value):
	blackboard_data.set_data(key,value)
func get_blackboard_data_dict() -> Dictionary:
	return blackboard_data.get_data_dict()
func clear_blackboard_data_dict():
	blackboard_data.clear_data_dict()
func build_fsm():
	fsm.init_fsm(self)
func translate_game_state(to_game_state,index:int=-1):
	_last_game_state = _cur_game_state
	_cur_game_state = to_game_state
	fsm.translate_state_by_state_name(get_name_from_game_state(to_game_state))
	GameEventSystem.on_game_state_changed.emit(_cur_game_state)
func translate_pre_game_state():
	translate_game_state(_last_game_state)
func get_last_game_state() -> GameEnum.GameState:
	return _last_game_state
func get_cur_game_state() -> GameEnum.GameState:
	return _cur_game_state
func get_name_from_game_state(game_state:GameEnum.GameState):
	return GameEnum.GameState.keys()[game_state]
func start_new_game():
	GameUISystem.view_manager.close_view(GameEnum.ViewType.MainMenuView)
	var room:BaseRoom = GameMapSystem.generate_map_by_type(GameEnum.RoomType.DevRoom)
	GamePlayerSystem.init_player()
	GameUISystem.view_manager.open_view(GameEnum.ViewType.GamingView)
func quit_game():
	GameStorageSystem.save_all()
	get_tree().quit()
